package com.study.spmongodb.wwj.thread2.char16;

import java.util.Random;

/**
 * @author study
 * @version 1.0
 * @date 2020/12/9 10:01
 */
public class CounterIncrement extends Thread {

    private final static Random random = new Random(System.currentTimeMillis());
    private volatile boolean terminated = false;
    private int counter = 0;

    @Override
    public void run() {

        try {
            while (!terminated) {
                System.out.println(Thread.currentThread().getName() + "-->" + counter++);
                Thread.sleep(random.nextInt(1000));
            }
        } catch (InterruptedException e) {
        } finally {
            this.clean();
        }
    }

    public void clean() {
        System.out.println("do some clean work for second pase");

    }

    public void close() {
        this.terminated = true;
        this.interrupt();
    }

}
